package com.tang.thepath;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.tang.thepath.service.JWTHolder;
import com.tang.thepath.util.ToastUtil;
import com.tang.thepath.util.UtilPicSelector;
import com.tang.thepath.view.ImageUploader;

import java.util.List;

public class BaseActivity extends AppCompatActivity{


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar mActionBar=getSupportActionBar();
        if (mActionBar != null) {
            mActionBar.setHomeButtonEnabled(true);
            mActionBar.setDisplayHomeAsUpEnabled(true);
        }
        //禁止横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //检测如果是从历史任务中启动，启动的activity可能不是LoginActivity
        if(this.getClass() != LoginActivity.class) {
            String token = JWTHolder.getInstance().getToken();
            if(token == null || token.equals("")){
                finish();
                startActivity(new Intent(getContext(),LoginActivity.class));
            }
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {//点击的是返回键
            if(isTaskRoot()){
                toast("程序已在后台运行");
                moveTaskToBack(true);
            }else{
                finish();
            }

        }
        return super.dispatchKeyEvent(event);
    }

    private KProgressHUD loadingRef;

    public void toast(String text){
        ToastUtil.showShort(text);
    }

    public void openGallery(int mode, List<LocalMedia> selected){
        UtilPicSelector.openGallery(this,9,1,selected);
    }
    public void openGallery(int mode,int maxCount,List<LocalMedia> selected){
        UtilPicSelector.openGallery(this,maxCount,1,selected);
    }
    public void openGallery(int mode,int maxCount,int minCount,List<LocalMedia> selected){
        UtilPicSelector.openGallery(this,maxCount,minCount,selected);
    }

    public void openCamera(int mode,List<LocalMedia> selected){
        UtilPicSelector.openCamera(this,9,1,selected);
    }
    public void openCamera(int mode,int maxCount,List<LocalMedia> selected){
        UtilPicSelector.openCamera(this,maxCount,1,selected);
    }
    public void openCamera(int mode,int maxCount,int minCount,List<LocalMedia> selected){
        UtilPicSelector.openCamera(this,maxCount,minCount,selected);
    }



    public void showLoading(String text,String detail,boolean cancelable){
        loadingRef = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel(text)
                .setCornerRadius(5f)
                .setDetailsLabel(detail)
                .setCancellable(cancelable)
                .setAnimationSpeed(1)
                .setDimAmount(0.3f)
                .show();
    }

    public void showLoading(String text,boolean cancelable){
        showLoading(text,null,cancelable);
    }

    public void showLoading(String text,String detail){
        showLoading(text,detail,false);
    }

    public void showLoading(String text){
        showLoading(text,null,false);
    }

    public void dismisLoading(){
        if(loadingRef != null && loadingRef.isShowing()){
            loadingRef.dismiss();
        }
    }

    public Context getContext(){
        return this;
    }

    public String getTag(){
        return this.getClass().getSimpleName();
    }


    public ImageUploader getImageUploader(){
        return null;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    getImageUploader().refresh(selectList);
                    break;
            }
        }else if(resultCode == GalleryActivity.RESULT){
            List<LocalMedia> selectList = (List<LocalMedia>)data.getSerializableExtra("result");
            getImageUploader().refresh(selectList);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    public void hideActionBar(){
        ActionBar mActionBar=getSupportActionBar();
        if (mActionBar != null) {
            mActionBar.hide();
        }
    }
}
